knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.4     ✓ stringr 1.4.0
## ✓ readr   2.1.1     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(here)
## here() starts at /Users/sachishiroma/Documents/ESM 244 - Lab/esm244-w2022-lab1
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.2.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(tmap)
sf_trees <- read_csv(here("data", "sf_trees", "sf_trees.csv"), show_col_types = FALSE)

To view the column names in data set type ‘names(sf_trees)’ in console

To view information of observations and diff tpyes ‘summary(sf_trees)’ in console

Part 1: Wragnling and GGplot review

Example Find counts of observation by ‘legal_status’ and wrangle a bit

### method 1: group_by() %>% summarize()
sf_trees %>% 
  group_by(legal_status) %>% 
  summarize(tree_count = n())
## # A tibble: 10 × 2
##    legal_status                 tree_count
##    <chr>                             <int>
##  1 DPW Maintained                   141725
##  2 Landmark tree                        42
##  3 Permitted Site                    39732
##  4 Planning Code 138.1 required        971
##  5 Private                             163
##  6 Property Tree                       316
##  7 Section 143                         230
##  8 Significant Tree                   1648
##  9 Undocumented                       8106
## 10 <NA>                                 54
### method 2: different way plus a few new functions
top_5_status <- sf_trees %>% 
  count(legal_status) %>% # gets count number by group 'legal_status'
  drop_na(legal_status) %>% # drops any row in the 'legal_status' column
  rename(tree_count = n) %>% # renames column name to 'n'
  relocate(tree_count) %>% # reorders columns and brings the "tree_count' column to # the front (far left)
  slice_max(tree_count, n = 5) %>% # takes top 5 highest number of trees
  arrange(-tree_count) # arranges tree count from highest to lowest 
# if you want to do lowest to highest take out '-' sign

Make a graph of the top 5 trees:

ggplot(data = top_5_status, aes(x = fct_reorder(legal_status, tree_count), y = tree_count)) + # `fct_reorder(legal_status, tree_count)` reorders the trees on graph from lowest to highest # add `-` sign in front of `tree_count` to do highest to lowest
  geom_col(fill = 'darkgreen') +
  labs(x = 'Legal Status', y = 'Tree Count') +
  coord_flip() + # rotate the columns horizontally so they're sideways 
  theme_minimal()

Example 2: Only going to keep observations where legal status is “permitted Site” and caretaker is “MTA”, and store as permitted_data_df

shift- cmd- c to comment/uncomment quickly

# sf_trees$legal_status %>% unique()
# unique(sf_trees$caretaker)
permitted_data_df <- sf_trees %>% 
  filter(legal_status == 'Permitted Site', caretaker == 'MTA') # keeps only variables in legal_status column that are `Permitted Site` or `MTA` 
# `|` means and/or `,` means and 

Example 3 Only keep Blackwood Acacia trees, and then only keep columns legal_status, date, latitude, longitude, and store as black_wood_acacia_df

black_wood_acacia_df <- sf_trees %>% 
  filter(str_detect(species, 'Blackwood Acacia')) %>% # `str_detect` says in this column look for 'Blackwood Acacia'
  select(legal_status, date, lat = latitude, lon = longitude) # keep only columns of `legal_status`, `date`, `longitude and latitude`
# also change latitude and longitude to lat and lon` 

# Make a little graph of location 
ggplot(data = black_wood_acacia_df, aes(x= lon, y = lat)) +
  geom_point(color = 'darkgreen')
## Warning: Removed 27 rows containing missing values (geom_point).

Example 4 Use tidyr::separate()

sf_trees_sep <- sf_trees %>% 
  separate(species, into =c('specific', 'spp_common'), sep = ' :: ') # Within Species column, Using ' :: ' separate left side of :: as `specific` column, and right side of :: as `spp column` 
## Warning: Expected 2 pieces. Missing pieces filled with `NA` in 15086 rows [1, 2,
## 3, 7, 17, 20, 30, 31, 33, 36, 38, 39, 41, 42, 43, 45, 46, 47, 48, 50, ...].

Example 5 Use tidyr::unite()

ex_5 <- sf_trees %>% 
  unite('id_status', tree_id, legal_status, sep = '_COOL_') 
# In a new column, 'id_status', combine 'tree_id' and 'legal_status' with '_COOL_' in between (drops column of tree_id and legal_status)
# You can combine them with just a space in between by`sep = ' '

Part 2: make some maps

Step 1:convert the lat/lon to spatial points, sf_as_sf()

black_wood_acacia_sf <- black_wood_acacia_df %>% 
  drop_na(lon, lat) %>% 
  st_as_sf(coords = c('lon', 'lat'))

###tell r what the coordinate reference system is 
st_crs(black_wood_acacia_sf) <- 4326

ggplot(data = black_wood_acacia_sf) +
  geom_sf(color = 'darkgreen')+
  theme_minimal()

# You do not need to assign aes because `geom_sf` has assigned aes 

Putting a map underneath the graph above Read in the SF shapefile and add to map

sf_map <-read_sf(here('data', 'sf_map', 'tl_2017_06075_roads.shp'))
sf_map_transform <- st_transform(sf_map, 4326)

ggplot(data = sf_map_transform)+
geom_sf()

Combine the Maps

ggplot() +
  geom_sf(data = sf_map, #this will be the first or bottom layer of the graph, everything else will be stacked on top 
          size = .1, # Change thickness of lines 
          color = 'darkgrey') +
  geom_sf(data = black_wood_acacia_sf, 
          color = 'red',
          size = 0.5)+
  theme_void()+
  labs(title = 'Blackwood Acacia in SF')

Now an interactive map !

tmap_mode('view')
## tmap mode set to interactive viewing
tm_shape(black_wood_acacia_sf)+
  tm_dots()
# tm_shape is equivalent to ggplot() and tm_dots is equivalent to geom_sf